﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;
using LeadVinaglue.Repository;
using LeadVinaglue.Utility;

namespace LeadVinaglue.Controllers
{
    public class ForgotPasswordController : BaseController
    {
        EFRoleProvider provider = new EFRoleProvider();
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        // GET: /Register/
        [DeviceFilterAttribute(Order = 1)]
        public ActionResult Index()
        {
            RegisterManagementRepository repository = new RegisterManagementRepository();
            RegisterManagementModel model = new RegisterManagementModel();

            // Setup common
            SetupCommon(model);

            repository.GetModel(ref model, LeadConstant.REGISTER);
            ViewBag.ErrorMessage = "";

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(FormCollection form, string returnUrl)
        {
            LogOnModel log = new LogOnModel();

            log.UserName = form["Email"].ToString();

            var user = new UserRepository().GetSingle(log.UserName);

            if (user != null)
            {
                bool success = false;
                UserEmailToken.SendEmail(log.UserName, "forgotpassword", ref success);
                ViewBag.ErrorMessage = "Thông tin mật khẩu vừa được gửi vào địa chỉ email của bạn.";
            }
            else
            {
                ViewBag.ErrorMessage = "Địa chỉ e-mail vừa nhập không tồn tại.";
            }

            // If we got this far, something failed, redisplay form
            RegisterManagementRepository repository = new RegisterManagementRepository();
            RegisterManagementModel model = new RegisterManagementModel();

            // Setup common
            SetupCommon(model);

            repository.GetModel(ref model, LeadConstant.REGISTER);

            return View(model);
        }
    }
}
